package ec.cd.processor.gui;

import java.awt.BorderLayout;
import java.awt.EventQueue;
import java.awt.FlowLayout;
import java.awt.GridLayout;
import java.awt.Panel;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.ButtonGroup;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JRadioButton;
import javax.swing.JTextField;
import javax.swing.border.Border;
import javax.swing.border.EmptyBorder;

public class MainFrame extends JFrame {

	private JPanel contentPane;
	private JPanel radioPanel;
	private JPanel fileChoosingPanel;
	private JRadioButton radioButtonCalibrationMode;
	private JRadioButton radioButtonTraceProcessingMode;
	private PanelChooseFile panelChooseFileCalibSitting;
	private PanelChooseFile panelChooseFileCalibWalking;
	private PanelChooseFile panelChooseFileCalibRunning;
	private PanelChooseFile panelChooseFileTrace;
	private PanelChooseFile panelChooseRangesAcc;
	private PanelChooseFile panelChooseRangesGyr;
	private JTextField textFieldQ;//for getting tolerance range
	private JLabel lblGiveQ;
	private JPanel panelInputQ;
	private JButton buttonProceed;

	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					MainFrame frame = new MainFrame();
					frame.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	/**
	 * Create the frame.
	 */
	public MainFrame() {
		contentPane = new JPanel();
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		contentPane.setLayout(new BorderLayout());
		setContentPane(contentPane);
		textFieldQ = new JTextField(15);
		lblGiveQ = new JLabel("Give q: ");
		panelInputQ = new JPanel(new FlowLayout());
		panelInputQ.add(lblGiveQ);
		panelInputQ.add(textFieldQ);		
		
		radioPanel = new JPanel();
		fileChoosingPanel = new JPanel();
		fileChoosingPanel.setLayout(new GridLayout(4,1));
		radioButtonCalibrationMode = new JRadioButton("Calibration Mode");
		radioButtonTraceProcessingMode = new JRadioButton("Trace Processing Mode");
		
		radioButtonCalibrationMode.addActionListener(new MyRadioActionListener(this));
		radioButtonTraceProcessingMode.addActionListener(new MyRadioActionListener(this));
		radioButtonCalibrationMode.setSelected(true);
		panelChooseFileCalibRunning = new PanelChooseFile("Running: ");
		panelChooseFileCalibWalking = new PanelChooseFile("Walking: ");
		panelChooseFileCalibSitting = new PanelChooseFile("Sitting: ");
		panelChooseFileTrace = new PanelChooseFile("Trace File: ");
		panelChooseRangesAcc = new PanelChooseFile("Ranges Acc: ");
		panelChooseRangesGyr = new PanelChooseFile("Ranges Gyr: ");
		buttonProceed = new JButton("Proceed...");
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setBounds(100, 100, 550, 300);
		
		ButtonGroup buttonGroupState = new ButtonGroup();
		buttonGroupState.add(radioButtonCalibrationMode);
		buttonGroupState.add(radioButtonTraceProcessingMode);
		radioPanel.setLayout(new FlowLayout());
		radioPanel.add(radioButtonCalibrationMode);
		radioPanel.add(radioButtonTraceProcessingMode);
		fileChoosingPanel.add(panelChooseFileCalibSitting);
		fileChoosingPanel.add(panelChooseFileCalibWalking);
		fileChoosingPanel.add(panelChooseFileCalibRunning);
		buttonProceed.addActionListener(new ActionListener() {
			
			@Override
			public void actionPerformed(ActionEvent arg0) {
				// TODO Auto-generated method stub
				if(radioButtonCalibrationMode.isSelected()){
					FrameResultsAndGraph frameResultsAndGraph = new FrameResultsAndGraph(0, panelChooseFileCalibSitting.getFileChosen(), panelChooseFileCalibWalking.getFileChosen(), panelChooseFileCalibRunning.getFileChosen(), 0);
					frameResultsAndGraph.setVisible(true);
				} else {
					FrameResultsAndGraph frameResultsAndGraph = new FrameResultsAndGraph(1, panelChooseFileTrace.getFileChosen(), panelChooseRangesAcc.getFileChosen(), panelChooseRangesGyr.getFileChosen(), 0);
					frameResultsAndGraph.setVisible(true);
				}
			}
		});
		contentPane.add(radioPanel, BorderLayout.NORTH);
		contentPane.add(buttonProceed, BorderLayout.SOUTH);
		contentPane.add(fileChoosingPanel, BorderLayout.CENTER);
		this.pack();
	}
	
	private class MyRadioActionListener implements ActionListener {
		JFrame frame;
		public MyRadioActionListener(JFrame frame) {
			// TODO Auto-generated constructor stub
			this.frame = frame;
		}
		@Override
		public void actionPerformed(ActionEvent e) {
			// TODO Auto-generated method stub
			if(radioButtonCalibrationMode.isSelected()){
				fileChoosingPanel.removeAll();
				fileChoosingPanel.add(panelChooseFileCalibSitting);
				fileChoosingPanel.add(panelChooseFileCalibWalking);
				fileChoosingPanel.add(panelChooseFileCalibRunning);
			} else {
				fileChoosingPanel.removeAll();
				fileChoosingPanel.add(panelChooseFileTrace);
				fileChoosingPanel.add(panelChooseRangesAcc);
				fileChoosingPanel.add(panelChooseRangesGyr);
				//fileChoosingPanel.add(panelInputQ);
			}
			frame.pack();
			frame.repaint();
		}
	}
	
	


}

